def solution(x, a):
    l = len(a)  
    s = set() 
    r = -1 # return
    for i,e in enumerate(a):
        s.add(e)     
        if(len(s)==x):
            return i   
    return -1



# print(solution(5, [1, 3, 1, 4, 2, 3, 5, 4]))  # 输出: 6
# print(solution(3, [1, 3, 1, 3, 2, 1, 3]))  # 输出: 4
# print(solution(2, [1, 2, 2, 1, 2]))  # 输出: 1
# print(solution(5, [3, 3, 3, 3, 3, 3]))  # 输出: -1
# print(solution(1, [1]))  # 输出: 0






def solution2(x, a):
    l = len(a)
    s = set(range(1, x + 1))  # 创建一个包含从1到x的所有整数的集合
    for i, e in enumerate(a):
        if e in s:
            s.remove(e)  # 从集合s中移除已经看到的元素
        if not s:
            return i  # 如果集合s为空，返回当前索引
    return -1  # 如果遍历完数组但未找到满足条件的前缀，返回-1

# 测试用例
print(solution(3, [6, 5, 4, 1, 2, 3, 5, 4]))  # 输出: 5

print(solution2(3, [6, 5, 4, 1, 2, 3, 5, 4]))  # 输出: 2